#!/usr/bin/env python

import os, sys, getopt
import csv

import utility


# given file index,
# return correspondence file name
def generate_file_name_by_index(index):
    file_name_prefix = "part-00"
    file_name_suffix = "-of-00500.csv"

    if index < 10:
        file_name = file_name_prefix+"00"+str(index)+file_name_suffix
    elif index < 100:
        file_name = file_name_prefix+"0"+str(index)+file_name_suffix
    else:
        file_name = file_name_prefix+str(index)+file_name_suffix
    return file_name


# given task_usage, save it to correspondence file
def write_task_usage_to_file(task_usage, output_prefix):
    statistics = {}
    for (job_id, task_id, start_time, end_time) in task_usage:
        if job_id not in statistics:
            statistics[job_id] = []
        statistics[job_id].append((task_id, start_time, end_time))

    for (job_id, job_records) in statistics.items():
        target_file = output_prefix+str(job_id)        
        with open(target_file, "a+") as f:
            for record in job_records:
                content = "".join([str(value)+" " for value in record])+"\n"
                f.write(content)


# main framework that trigger the process
def process_data(begin_index, end_index):
    task_usage_prefix = "task_usage/"
    
    output_prefix = "output_task_usage/"
    if not os.path.exists(output_prefix):
        os.system("mkdir "+output_prefix)

    for i in range(begin_index, end_index):
        task_usage_file_name = task_usage_prefix+generate_file_name_by_index(i)
        task_usage = utility.extract_task_usage(task_usage_file_name)
        write_task_usage_to_file(task_usage, output_prefix)


def main(argv):
    try:
        opts, args = getopt.getopt(argv[1:], "hb:e:")
    except getopt.GetoptError as err:
        print(err)
        sys.exit(2)

    # set default value
    begin_index = 0
    end_index = 500

    for o, s in opts:
        # help information
        if o == "-h":
            print "usge   -b <begin package index>"
            print "       -e <end package index>"
            return
        elif o == "-b":
            begin_index = int(s)
        elif o == "-e":
            end_index = int(s)

    process_data(begin_index, end_index)
    

if __name__ == "__main__":
    main(sys.argv)